package com.net;

import org.junit.Test;

import java.io.FileDescriptor;
import java.io.IOException;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.net.ServerSocket;
import java.net.Socket;
import java.net.SocketImpl;

/**
 * Created by yangliwei on 2017/7/19.
 */
public class ServerSocketDemo {
    public static void main(String[] args) throws  Exception{
        ServerSocket server = null;
        Socket socket;
        byte[] rb = new byte[1024];
        byte[] wb = new byte[1024];
        try {
            server = new ServerSocket(8888);

            getFD(server);

            while(true){
                socket = server.accept();//block
                System.out.println("---------------------");
//                socket.getInputStream().read(rb);
                getFD(socket);
                boolean flag = true;
                boolean start = true;
                while(flag){
//                    if(start){
//                        System.out.println("start");
//                        start = false;
//                    }
////                    System.out.println(rb);
                    int i = socket.getInputStream().read();
                    if(i ==-1){
                        flag = false;
                    }
                    System.out.println(i);
                }
//                socket.getOutputStream().write('A');

            }
        }
        catch (IOException e) {
            e.printStackTrace();
        }
        finally {
            try {
                server.close();
            }
            catch (IOException e) {
                e.printStackTrace();
            }
        }
    }


    public static  void getFD(Socket socket)throws Exception{
        Field implField = Socket.class.getDeclaredField("impl");
        implField.setAccessible(true);
        SocketImpl socketImpl = (SocketImpl) implField.get(socket);

        Method method = SocketImpl.class.getDeclaredMethod("getFileDescriptor");
        method.setAccessible(true);
        FileDescriptor fd = (FileDescriptor)method.invoke(socketImpl);
        Field fdField = FileDescriptor.class.getDeclaredField("fd");
        fdField.setAccessible(true);
        System.out.println(fdField.get(fd));
    }

    public static  void getFD(ServerSocket socket)throws Exception{
        Field implField = ServerSocket.class.getDeclaredField("impl");
        implField.setAccessible(true);
        SocketImpl socketImpl = (SocketImpl) implField.get(socket);

        Method method = SocketImpl.class.getDeclaredMethod("getFileDescriptor");
        method.setAccessible(true);
        FileDescriptor fd = (FileDescriptor)method.invoke(socketImpl);
        Field fdField = FileDescriptor.class.getDeclaredField("fd");
        fdField.setAccessible(true);
        System.out.println(fdField.get(fd));
    }


    /**
     * 代理socket服务器
     */
    @Test
    public void testProxyServer(){
//        ServerSocket serverSocket = new ServerSocket();
    }
}
